//
//  THTaskDetailVC.m
//  YJB
//
//  Created by 中电大维 on 2016/11/12.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THTaskDetailVC.h"
#import "THTasklistModel.h"
#import "THBottomAlterView.h"
#import "YJNetworkTool.h"
#import "YJUserDefaultTool.h"

@interface THTaskDetailVC ()
@property (weak, nonatomic) IBOutlet UILabel *taskTitle;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *url;
@property (weak, nonatomic) IBOutlet UILabel *total;
@property (weak, nonatomic) IBOutlet UILabel *taskCount;
@property (weak, nonatomic) IBOutlet UILabel *completeNUm;
@property (weak, nonatomic) IBOutlet UILabel *state;

@end

@implementation THTaskDetailVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"任务详情";
    _taskTitle.text = _model.title;
    _time.text = [_model.createTime stringWithDateFormat:@"yyyy-MM-dd"];
    _url.text = _model.url;
    _total.text = _model.goldTotal;
    _taskCount.text = _model.taskCount;
    _completeNUm.text = _model.surfedcount.length?_model.surfedcount:@"0";
    _state.text = _model.status.integerValue == 0?@"正常":@"非正常";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
}

- (void)deleteClick
{
    YJWeakSelf
    [THBottomAlterView showWithCheckBlock:^{
        [weakSelf deleteTask];
    }];
}

- (void)deleteTask
{
    [MBProgressHUD showMessage:@"正在删除" toView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *userName = [YJUserDefaultTool readUserName];
    NSString *md5Key = [YJUserDefaultTool readKeyID];
    params[@"action"] = @"del_traff_one";
    params[@"makegold"] = @{@"username":userName,
                            @"task_id":_model.ID,
                            @"user_info":userName,
                            @"phone_info":@"iOS",
                            @"md5_key":md5Key};
    [YJNetworkTool postWithUrl:THURL(@"traffic.php") parameters:params success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        NSString *code = responseObject[@"return"][@"code"];
        if (code.integerValue == 200)
        {
            if (self.taskDeleteSuccessBlock)
                self.taskDeleteSuccessBlock();
            [MBProgressHUD showError:@"删除成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            NSString *message = responseObject[@"return"][@"message"];
            [MBProgressHUD showError:message toView:self.view];
        }
    } failure:^(NSError *error)
     {
         [MBProgressHUD hideHUDForView:self.view];
         [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
     }];
}






@end
